package com.huaxin.common.message.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

/**
 * Created by hebing on 2016/9/13.
 */
public class StaticSearchView extends RelativeLayout {
    public StaticSearchView(Context context) {
        super(context);
    }

    public StaticSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StaticSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public StaticSearchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    int dx, dy;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                dx = (int) event.getX();
                dy = (int) event.getY();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int cx = (int) event.getX();
                int cy = (int) event.getY();
                if (Math.abs(cx - dx) < 10 && Math.abs(cy - dy) < 10) {
                    if (callBack != null) callBack.onClick();
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onClick();
    }
}
